<?php
/**
 * @file
 * Creates user interface for adding data to the individuals table.
 * @version 8/9/11
 * @author Matt Vartanian
 */
include_once ('../db-control/dbDonor.php');
include_once ('../db-control/dbIndividual.php');
include('header.php');

addIndDonor();

/**
 * @function addIndDonor
 * Calls all other functions and creates addIndDonor.php UI.
 * $inputData array holds $_POST information
 * $errorArray array holds error information
 */
function addIndDonor()
{
	$inputData = array('firstName'=>"", 'lastName'=>"", 'street'=>"",
    'city'=>"", 'stateName'=>"",'zip'=>"", 'phone'=>"", 'fax'=>"", 'email'=>"");

	$errorArray = array('firstNameError'=>"", 'lastNameError' =>"",
    'streetError' => "", 'cityError'=>"", 'stateNameError'=>"", 'zipError'=>"", 
    'phoneError' =>"", 'faxError'=>"", 'emailError'=>"");
	if(isset($_GET['id'])) {
		placeUserInfoInForm($inputData);
	}
	individualDonorHeader( );
	if (isset($_POST['enterDonor'])){
		checkEnterIndividualButton($inputData, $errorArray);
	}
	if (isset($_POST['clearFields'])) {
		checkClearFieldsButton();
	}
	viewAddIndDonorForm($inputData, $errorArray);
}

/**
 * @function checkEnterIndividualButton
 * Checks if the enter donor button has been pressed. Performs error
 * checking and requires all fields to be filled in before submitting data to
 * database.
 * @param array $inputData array of $_POST data
 * @param string $donorName name of newly entered individual
 */
function checkEnterIndividualButton(&$inputData, &$errorArray)
{
	$id = 0;
	$allFieldsFilled=true;
	areFieldsFilled($allFieldsFilled, $errorArray);

	if($allFieldsFilled == true) {
		getPostVars($inputData);

		dbDonor::addDonorByFieldsInArray($inputData, $id);
		dbIndividual::addIndividualByFields($id, $inputData['firstName'],
		$inputData['lastName']);

		echo $inputData['firstName'] . " " . $inputData['lastName']."
			has been entered into the database.";
	}
	else { // submit was pressed, but some fields are empty
		echo "<font color=FF0000> * indicates required fields,
			please re-enter. <br /></font>";
		getPostVars($inputData);
	}
}

/**
 * Places $_GET info in form.
 */
function placeUserInfoInForm(&$inputData)
{
	$id = $_GET['id'];
		$individualData = dbIndividual::getIndividualById($id);
		$individualObject = new Individual($individualData['donorID'],
										   $individualData['firstName'],
										   $individualData['lastName']);
		$donorData = dbDonor::getDonorById($id);
		$donorObject = new Donor($donorData);
		
		$inputData['firstName'] = $individualObject->getFirstName( );
		$inputData['lastName'] = $individualObject->getLastName( );
		$inputData['street'] = $donorObject->getStreet( );
		$inputData['city'] = $donorObject->getCity( );
		$inputData['stateName'] = $donorObject->getStateName( );
		$inputData['zip'] = $donorObject->getZip( );
		$inputData['phone'] = $donorObject->getPhone( );
		$inputData['fax'] = $donorObject->getFax( );
		$inputData['email'] = $donorObject->getEmail( );
}

/** 
 * Checks to see if the Clear Fields button has been pressed. If it has been
 * pressed, resets page.
 */
function checkClearFieldsButton()
{
}

/**
 * Creates the form for entering individual data.
 * @param $inputdata is an array of $_POST variables entered by the user.
 * @param $firstNameError records whether the corresponding field has data
 * entered in it or not.
 *
 */
function viewAddIndDonorForm($inputData, $errorArray)
{
	$heading = "";
	if ($inputData['firstName'] == "") {
		$heading = "New Individual Donor";
	} else {
		$heading = "Edit Information";
	}
	echo <<<_END
	<h3>$heading</h3>
  <form method="post" action="addIndDonor.php"><pre>
    First name  <input type="text" name="firstName"
    value="$inputData[firstName]"/> {$errorArray['firstNameError']}
    Last name   <input type="text" name="lastName" 
    value="$inputData[lastName]"/> {$errorArray['lastNameError']}
    Street      <input type="text" name="street" 
    value="$inputData[street]"/> {$errorArray['streetError']}
    City        <input type="text" name="city" 
    value="$inputData[city]"/> {$errorArray['cityError']}
    <br />
_END;
	state($inputData, $errorArray);
	echo <<<_END
    Zip    <input type="text" size="8" maxlength="8" name="zip" 
    value="$inputData[zip]" /> {$errorArray['zipError']}
    Phone       <input type="text" name="phone" size="32" 
    value="$inputData[phone]" /> {$errorArray['phoneError']}
    Fax         <input type="text" name="fax" size="32" 
    value="$inputData[fax]"/> {$errorArray['faxError']}
    E-mail      <input type="text" name="email" size="32" 
    value="$inputData[email]"/> {$errorArray['emailError']}</pre>
    <input type="submit" value="Save" name="enterDonor" />
    <input type="submit" value="Cancel" name="clearFields" />  
  </form>
  <form method="post" action="individualdonorv1.php">
  <input type="submit" value="<- Back" name="backButton"/>
  </form>
_END;
}

/**
 * Retrieves the data entered by the user into the fields represented.
 * @param $inputData is an array of $_POST variables entered by the user.
 */
function getPostVars(&$inputData)
{
	extract($_POST);
	$inputData = compact('firstName', 'lastName', 'street', 'city', 'stateName',
	 'zip', 'phone', 'fax', 'email');
}

/**
 * Checks that all fields have values entered. If there are empty fields,
 * corresponding error variables are assigned value of "*".
 *
 * @param $allFieldsFilled is a boolean value which determines whether or not
 * the user has neglected to enter data into any of the available fields.
 * @param $errorArray is an array of all the * error messages.
 */

function areFieldsFilled(&$allFieldsFilled, &$errorArray)
{
	if(trim($_POST['firstName']) == ""){
		$errorArray['firstNameError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['lastName'])== ""){
		$errorArray['lastNameError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['street'])== ""){
		$errorArray['streetError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['city'])== ""){
		$errorArray['cityError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['zip'])== ""){
		$errorArray['zipError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['phone'])== ""){
		$errorArray['phoneError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['fax'])== ""){
		$errorArray['faxError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['email'])== ""){
		$errorArray['emailError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
	if(trim($_POST['stateName'])== ""){
		$errorArray['stateNameError']="<font color=FF0000>*</font>";
		$allFieldsFilled=false;
	}
}

/**
 * Creates a list box for state selection.
 */
function state($inputData, $errorArray)
{
	echo <<<_END
    State Name  <select name="stateName">
        <option value="{$inputData['stateName']}" selected="selected">{$inputData['stateName']}</option>
        <option value="AL">AL</option>
        <option value="AK">AK</option>
        <option value="AZ">AZ</option>
        <option value="AR">AR</option>
        <option value="CA">CA</option>
        <option value="CO">CO</option>
        <option value="CT">CT</option>
        <option value="DE">DE</option>
        <option value="DC">DC</option>
        <option value="FL">FL</option>
        <option value="GA">GA</option>
        <option value="HI">HI</option>
        <option value="ID">ID</option>
        <option value="IL">IL</option>
        <option value="IN">IN</option>
        <option value="IA">IA</option>
        <option value="KS">KS</option>
        <option value="KY">KY</option>
        <option value="LA">LA</option>
        <option value="ME">ME</option>
        <option value="MD">MD</option>
        <option value="MA">MA</option>
        <option value="MI">MI</option>
        <option value="MN">MN</option>
        <option value="MS">MS</option>
        <option value="MO">MO</option>
        <option value="MT">MT</option>
        <option value="NE">NE</option>
        <option value="NV">NV</option>
        <option value="NH">NH</option>
        <option value="NJ">NJ</option>
        <option value="NM">NM</option>
        <option value="NY">NY</option>
        <option value="NC">NC</option>
        <option value="ND">ND</option>
        <option value="OH">OH</option>
        <option value="OK">OK</option>
        <option value="OR">OR</option>
        <option value="PA">PA</option>
        <option value="RI">RI</option>
        <option value="SC">SC</option>
        <option value="SD">SD</option>
        <option value="TN">TN</option>
        <option value="TX">TX</option>
        <option value="UT">UT</option>
        <option value="VT">VT</option>
        <option value="VA">VA</option>
        <option value="WA">WA</option>
        <option value="WV">WV</option>
        <option value="WI">WI</option>
        <option value="WY">WY</option>
        </select>{$errorArray['stateNameError']}
_END;
}

?>